package com.han.shadow.customerapp.fragment;

import android.app.DialogFragment;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.han.shadow.customerapp.R;
import com.han.shadow.customerapp.bean.Goods;
import com.han.shadow.customerapp.mystatic.MyScreen;

import java.util.List;


/**
 * Created by ACE on 2016/4/22 0022.
 */
public class GoodsInfoFragment extends DialogFragment {


    private final Handler handlerParent;
    private final List<Bitmap> bms;
    private View view = null;
    private Goods goods = null;


    private ImageView goodsInfoImage = null;
    private TextView goodsInfoName = null;
    private TextView goodsInfoPrice = null;
    private Button goodsInfoAddInCart = null;
    private Button goodsInfoPlus = null;
    private Button goodsInfoSub = null;
    private TextView goodsInfoCount = null;
    private TextView goodsInfoInformation = null;
    private int count = 1;

    int w;
    int h;


    public GoodsInfoFragment(Goods goods, List<Bitmap> bms, Handler handler){
        this.goods = goods;
        this.handlerParent = handler;
        this.bms = bms;

        w = MyScreen.dm.widthPixels;
        h = MyScreen.dm.heightPixels;
        //w = (int)(w-w*0.1);
        h = (int)(h-h*0.1);
    }


    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();

        window.setLayout(w,h);
        window.setGravity(Gravity.CENTER);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = inflater.inflate(R.layout.fragment_goodsinfo,null);

        initCtrl();
        initText();
        initClick();
        return view;
    }

    public void initCtrl(){
        goodsInfoImage = (ImageView)view.findViewById(R.id.goodsInfoImage);
        goodsInfoName = (TextView)view.findViewById(R.id.goodsInfoName);
        goodsInfoPrice = (TextView)view.findViewById(R.id.goodsInfoPrice);
        goodsInfoAddInCart = (Button)view.findViewById(R.id.goodsInfoAddInCart);
        goodsInfoCount = (TextView)view.findViewById(R.id.goodsInfoCount);
        goodsInfoPlus = (Button)view.findViewById(R.id.goodsInfoPlus);
        goodsInfoSub = (Button)view.findViewById(R.id.goodsInfoSub);
        goodsInfoInformation = (TextView)view.findViewById(R.id.goodsInfoInformation);


        goodsInfoImage.setImageBitmap(bms.get(0));
        goodsInfoImage.getLayoutParams().height = (int)(h*0.3);
        goodsInfoImage.getLayoutParams().width = (int)(w*0.6);

        goodsInfoName.setText(goods.getName());
        goodsInfoPrice.setText("价格"+(float)goods.getPrice()/10+"元");
        goodsInfoInformation.setText(goods.getInformation());
    }


    public void initText(){
        //image.setImageBitmap(new Bitmap());
        if(goods != null) {
            goodsInfoName.setText("名称：" + goods.getName());
            goodsInfoPrice.setText("价格：" + (float)goods.getPrice()/10 + " 元");
        }
        goodsInfoCount.setText(String.valueOf(count));
    }

    public void initClick(){
        goodsInfoAddInCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//添加购物车
                Message msg = new Message();
                Bundle bundle = new Bundle();
                bundle.putString("gID",goods.getGid());
                bundle.putInt("price", goods.getPrice());
                bundle.putInt("count",count);
                msg.setData(bundle);
                msg.what = 1;
                handlerParent.sendMessage(msg);
            }
        });

        goodsInfoPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsInfoCount.setText(String.valueOf(++count));
            }
        });

        goodsInfoSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(--count <= 0){
                    count = 0;
                }
                goodsInfoCount.setText(String.valueOf(count));
            }
        });
    }
}
